//
//  KKTrackTool.m
//  TrackDemo
//
//  Created by sanfang on 2024/5/9.
//

#import "KKTrackTool.h"

#import "KKPointModel.h"
#import "GPX.h"//GPX相关
#import "KML.h"//KML相关

#define DOC_PATH NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES).firstObject

@implementation KKTrackTool

+ (void)generateKMLFileFromPoints:(NSArray<KKPointModel *> *)gpsPoints
{
    KMLRoot *root = [[KMLRoot alloc] init];
    KMLDocument *doc = [[KMLDocument alloc] init];
    root.feature = doc;
    KMLFolder *folder = [[KMLFolder alloc] init];
    [doc addFeature:folder];
    KMLPlacemark *placemark = [[KMLPlacemark alloc] init];
    //轨迹名称
    NSString *trackName = @"trackName";
    placemark.name = @"trackName";
    placemark.descriptionValue = @"Generate By Heyou.sanfang.";
    [folder addFeature:placemark];
    
    KMLLineString *line = [[KMLLineString alloc] init];
    for (NSInteger i = 0; i < gpsPoints.count; i++)
    {
        KKPointModel *model = gpsPoints[i];
        KMLCoordinate *coordinate = [[KMLCoordinate alloc] init];
        coordinate.latitude = model.latitude;
        coordinate.longitude = model.longitude;
        [line addCoordinate:coordinate];
    }
    
    placemark.geometry = line;
    
    //加上时间戳
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    //文件名称
    NSString *fileNameTemp = [NSString stringWithFormat:@"%@%@.kml",trackName,@(time)];
    //文件路径
    NSString *path = [DOC_PATH stringByAppendingPathComponent:fileNameTemp];
    NSData *data = [root.kml dataUsingEncoding:NSUTF8StringEncoding];
    //写入文件
    BOOL success = [data writeToFile:path atomically:YES];
    NSLog(@"success:%d, path:%@", success, path);
}

+ (void)generateGPXFileFromPoints:(NSArray<KKPointModel *> *)gpsPoints
{
    GPXRoot *root = [GPXRoot rootWithCreator:@"GPX file Generate By Heyou.sanfang"];
    root.version = @"1.0";

    GPXTrack *track = [root newTrack];
    //轨迹名称
    NSString *trackName = @"trackName";
    track.name = trackName;
    for (NSInteger i = 0; i < gpsPoints.count; i++)
    {
        KKPointModel *point = gpsPoints[i];
        [track newTrackpointWithLatitude:point.latitude
                               longitude:point.longitude];
    }
    //加上时间戳
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    //文件名
    NSString *fileNameTemp = [NSString stringWithFormat:@"%@%@.kml",trackName,@(time)];
    //文件路径
    NSString *path = [DOC_PATH stringByAppendingPathComponent:fileNameTemp];
    NSData *data = [root.gpx dataUsingEncoding:NSUTF8StringEncoding];
    //写入文件
    BOOL success = [data writeToFile:path atomically:YES];
    NSLog(@"success:%d, path:%@", success, path);
}

+ (void)parseKMLFileData:(NSData *)data
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        KMLRoot *root = [KMLParser parseKMLWithData:data];
        if (root)
        {
            NSString *gpsString = root.kml;
            NSLog(@"kml gps str:%@",gpsString);
            //加载到地图上
        }
    });
}

+ (void)parseGPXFileData:(NSData *)data
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        GPXRoot *root = [GPXParser parseGPXWithData:data];
        if (root)
        {
            NSString *gpsString = root.gpx;
            NSLog(@"gpx gps str:%@",gpsString);
            //加载到地图上
        }
    });
}


@end
